import jdk.jshell.PersistentSnippet;

class Money implements Cloneable {
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable {

    // 当我们要实现自己类中的clone的时候要实现一个cloneable的接口
    // Cloneable 是一个空接口或者叫标记接口，表明当前的类是可以被克隆的
    public String name;
    public int age;

    public Money money = new Money();

    @Override
    public String toString() {
        return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();

        // 怎么克隆money呢
        Person tmp = (Person) super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;

    }

    @Override
    public boolean equals(Object obj) {
//        return super.equals(obj);
        Person tm = (Person) obj;
        return this.name.equals(tm.name) && this.age == tm.age;
    }
}

public class Test {
    public static void main(String[] args) {
        Person person = new Person("lisi", 10);
        Person person2 = new Person("lisi", 10);

        System.out.println(person.equals(person2));
    }

    public static void main2(String[] args) throws CloneNotSupportedException {
        Person person = new Person("lisi", 10);

        Person person2 = (Person) person.clone();

        System.out.println(person.money.m);
        System.out.println(person2.money.m);
        System.out.println("=======================");

        person2.money.m = 99.9;
        System.out.println(person.money.m);
        System.out.println(person2.money.m);
    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person = new Person("lisi", 10);

        Person person2 = (Person) person.clone();

        System.out.println(person);
        System.out.println(person2);
    }
}
